#!/bin/bash -e

USER="oss_admin"
DATABASE="oss"
PASSWORD='123'
FINISH_GUIDE=3
MYSQL_FILE_DIR="/var/lib/mysql-files"

MYSQL=`which mysql`
register=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select distinct(uid) from PlayerLogin where EventTime regexp '^2018-01-16' and AccountType != 4;
EOF`

login_17=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select uid from PlayerRegister r where EventTime regexp '^2018-01-16' and uid in(select distinct(uid) from PlayerLogin l  where EventTime regexp '^2018-01-17');
EOF`

login_18=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select uid from PlayerRegister r where EventTime regexp '^2018-01-16' and uid in(select distinct(uid) from PlayerLogin l  where EventTime regexp '^2018-01-18');
EOF`

login_19=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select uid from PlayerRegister r where EventTime regexp '^2018-01-16' and uid in(select distinct(uid) from PlayerLogin l  where EventTime regexp '^2018-01-19');
EOF`

login_20=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select uid from PlayerRegister r where EventTime regexp '^2018-01-16' and uid in(select distinct(uid) from PlayerLogin l  where EventTime regexp '^2018-01-20');
EOF`

login_21=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select uid from PlayerRegister r where EventTime regexp '^2018-01-16' and uid in(select distinct(uid) from PlayerLogin l  where EventTime regexp '^2018-01-21');
EOF`

login_22=`$MYSQL -u $USER --password=$PASSWORD  --show-warnings=false << EOF | tail -n +2
use $DATABASE;
select uid from PlayerRegister r where EventTime regexp '^2018-01-16' and uid in(select distinct(uid) from PlayerLogin l  where EventTime regexp '^2018-01-22');
EOF`

declare -A data

for uid in $register; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

for uid in $login_17; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

for uid in $login_18; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

for uid in $login_19; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

for uid in $login_20; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

for uid in $login_21; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

for uid in $login_22; do
    if [ -z "${data[$uid]}" ]; then
        data[$uid]=1
    else
        let data[$uid]++
    fi
done

count_2=0;
count_3=0;

for key in ${!data[@]}; do
    if [ ${data[$key]} -ge 2 ]; then
        let count_2+=1
    fi

    if [ ${data[$key]} -ge 3 ]; then
        let count_3+=1
    fi

done

echo $count_2 $count_3 $((count_3/count_2))
awk 'BEGIN{printf "%.2f\n",('$count_3'/'$count_2')}'
